package com.brillilab.service.core.global.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.global.GlobalReagentAntibodyMapper;
import com.brillilab.dao.mapper.global.GlobalReagentMapper;
import com.brillilab.dao.mapper.global.GlobalReagentPlasmidFragmentMapper;
import com.brillilab.dao.mapper.global.GlobalReagentPlasmidMapper;
import com.brillilab.dao.mapper.global.GlobalReagentPrimerAttrMapper;
import com.brillilab.dao.mapper.global.GlobalReagentPrimerMapper;
import com.brillilab.dao.mapper.global.GlobalSampleAttrMapper;
import com.brillilab.domain.enums.common.IsDeleteEnum;
import com.brillilab.domain.po.global.GlobalReagent;
import com.brillilab.domain.po.global.GlobalReagentAntibody;
import com.brillilab.domain.po.global.GlobalReagentPlasmid;
import com.brillilab.domain.po.global.GlobalReagentPlasmidFragment;
import com.brillilab.domain.po.global.GlobalReagentPrimer;
import com.brillilab.domain.po.global.GlobalReagentPrimerAttr;
import com.brillilab.domain.po.global.GlobalSampleAttr;
import com.brillilab.service.core.global.IGlobalReagentService;
import com.brillilab.service.core.system.IFileManageService;

@Service
public class GlobalReagentServiceImpl implements IGlobalReagentService {
	@Autowired
	private GlobalReagentMapper globalReagentMapper;
	@Autowired
	private GlobalReagentAntibodyMapper globalReagentAntibodyMapper;
	@Autowired
	private GlobalReagentPrimerMapper globalReagentPrimerMapper;
	@Autowired
	private GlobalReagentPrimerAttrMapper globalReagentPrimerAttrMapper;
	@Autowired
	private GlobalReagentPlasmidMapper globalReagentPlasmidMapper;
	@Autowired
	private GlobalReagentPlasmidFragmentMapper globalReagentPlasmidFragmentMapper;
	@Autowired
	private GlobalSampleAttrMapper globalSampleAttrMapper;
	@Resource
	private IFileManageService fileManageService;
	
	
	@Override
	public List<GlobalReagent> selectList(Long kitId) {
		LambdaQueryWrapper<GlobalReagent> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(GlobalReagent::getKitId,kitId);
		wrapper.eq(GlobalReagent::getIsDelete,IsDeleteEnum.NO.getValue());
		return globalReagentMapper.selectList(wrapper);
	}

	@Override
	public GlobalReagent selectById(Long id) {
		return globalReagentMapper.selectById(id);
	}
	
	@Override
	public GlobalReagentAntibody selectAntibody(Long kitId,Long reagentId) {
		LambdaQueryWrapper<GlobalReagentAntibody> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(GlobalReagentAntibody::getKitId,kitId);
		wrapper.eq(GlobalReagentAntibody::getReagentId,reagentId);
		return globalReagentAntibodyMapper.selectOne(wrapper);
	}
	
	@Override
	public GlobalReagentPrimer selectPrimer(Long kitId, Long reagentId) {
		LambdaQueryWrapper<GlobalReagentPrimer> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(GlobalReagentPrimer::getKitId,kitId);
		wrapper.eq(GlobalReagentPrimer::getReagentId,reagentId);
		GlobalReagentPrimer reagentPrimer = globalReagentPrimerMapper.selectOne(wrapper);
		if(reagentPrimer!=null){
			LambdaQueryWrapper<GlobalReagentPrimerAttr> attrWrapper = new LambdaQueryWrapper<>();
			attrWrapper.eq(GlobalReagentPrimerAttr::getKitId,kitId);
			attrWrapper.eq(GlobalReagentPrimerAttr::getReagentId,reagentId);
			List<GlobalReagentPrimerAttr> primerAttrList = globalReagentPrimerAttrMapper.selectList(attrWrapper);
			reagentPrimer.setPrimerAttrList(primerAttrList);
		}
		return reagentPrimer;
	}
	
	@Override
	public GlobalReagentPlasmid selectPlasmid(Long kitId, Long reagentId) {
		LambdaQueryWrapper<GlobalReagentPlasmid> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(GlobalReagentPlasmid::getKitId,kitId);
		wrapper.eq(GlobalReagentPlasmid::getReagentId,reagentId);
		GlobalReagentPlasmid reagentPlasmid = globalReagentPlasmidMapper.selectOne(wrapper);
		if(reagentPlasmid!=null){
			reagentPlasmid.setAbstractFileList(fileManageService.getFileList(reagentPlasmid.getAbstractFile()));
			reagentPlasmid.setAtlasFileList(fileManageService.getFileList(reagentPlasmid.getAtlasFile()));
			reagentPlasmid.setReportFileList(fileManageService.getFileList(reagentPlasmid.getReportFile()));
			reagentPlasmid.setOtherFileList(fileManageService.getFileList(reagentPlasmid.getOtherFile()));
			
			LambdaQueryWrapper<GlobalReagentPlasmidFragment> attrWrapper = new LambdaQueryWrapper<>();
			attrWrapper.eq(GlobalReagentPlasmidFragment::getKitId,kitId);
			attrWrapper.eq(GlobalReagentPlasmidFragment::getReagentId,reagentId);
			List<GlobalReagentPlasmidFragment> fragmentList = globalReagentPlasmidFragmentMapper.selectList(attrWrapper);
			reagentPlasmid.setFragmentList(fragmentList);
		}
		return reagentPlasmid;
	}
	
	@Override
	public List<GlobalSampleAttr> selectSampleAttrList(Long kitId, Long reagentId){
		LambdaQueryWrapper<GlobalSampleAttr> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(GlobalSampleAttr::getKitId,kitId);
		wrapper.eq(GlobalSampleAttr::getReagentId,reagentId);
		return globalSampleAttrMapper.selectList(wrapper);
	}
@Override
	public boolean updateById(GlobalReagent reagent){
		return globalReagentMapper.updateById(reagent)>0;
	}
}
